package com.chromatic.competition.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

/**
 * 网络工具类
 */
public class NetworkUtils {

    private final Context context;

    public NetworkUtils(Context context) {
        this.context = context;
    }

    /**
     * 检查网络连接状态并显示 Toast 提示
     */
    public void checkNetworkConnected() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
                boolean isConnected = networkInfo != null && networkInfo.isConnected();

                if (!isConnected) {
                    showToast("网络未连接");
                }
            }
        }).start();
    }

    /**
     * 检查互联网可用性并显示 Toast 提示
     */
    public void checkInternetAvailable() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Process process = Runtime.getRuntime().exec("/system/bin/ping -c 1 snake-konginchrist.com");
                    int exitCode = process.waitFor();
                    boolean isAvailable = exitCode == 0;
                    if (isAvailable) {
                        showToast("成功连接至服务器!");
                    } else {
                        showToast("无法连接至服务器!");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 显示 Toast 提示
     *
     * @param message 提示内容
     */
    private void showToast(final String message) {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 检查网络连接状态并显示 Toast 提示
     */
    public void isNetworkConnected() {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        boolean isConnected = networkInfo != null && networkInfo.isConnected();

        if (!isConnected) {
            Toast.makeText(context, "网络未连接", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 检查互联网可用性并显示 Toast 提示
     */
    public void isInternetAvailable() {
        try {
            Process process = Runtime.getRuntime().exec("/system/bin/ping -c 1 snake-konginchrist.com");
            int exitCode = process.waitFor();
            boolean isAvailable = exitCode == 0;
            if (isAvailable) {
                Toast.makeText(context, "成功连接至服务器!", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "无法连接至服务器!", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}